<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" > 

<html>
<head>
<base href="<%=basePath%>">
<title>管理员权限</title>
<link rel="stylesheet" href="./js/bootstrap-3.3.5-dist/css/bootstrap.css" type="text/css"/>
<link rel="stylesheet" href="./js/metisMenu/metisMenu.min.css">
<link rel="stylesheet" href="./css/sb-admin/sb-admin-2.css">
<link rel="stylesheet" href="./css/base.css">
<link rel="stylesheet" href="./css/illness.css">
<link rel="stylesheet" href="./js/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="./js/bootstrap-table/bootstrap-table.min.css">
<link rel="stylesheet" href="./js/bootstrap-multiselect/bootstrap-multiselect.css" type="text/css"/>
<link rel="stylesheet" href="./js/bootstrap-3.3.5-dist/css/bootstrap-datetimepicker.css">
<link rel="stylesheet" type="text/css" href="./js/ztree/css/zTreeStyle/zTreeStyle.css" />

<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="./js/ztree/jquery-migrate-1.2.1.js"></script>

<script type="text/javascript" src="./js/bootstrap-3.3.5-dist/js/bootstrap.js"></script>
<script type="text/javascript" src="./js/metisMenu/metisMenu.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script src="./js/bootstrap-table/bootstrap-table-all.min.js"></script>
<script src="./js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
<script src="./js/bootstrap-multiselect/bootstrap-multiselect.js"></script>
<script src="./js/bootstrap-jquery-plugin/jquery.bootstrap.js"></script>
<script src="./js/bootstrap-3.3.5-dist/js/bootstrap-datetimepicker.js"></script>
<script src="./js/bootstrap-3.3.5-dist/js/bootstrap-datetimepicker.zh-CN.js"></script>

<!-- <script type="text/javascript" src="./js/ztree/jquery-1.4.4.min.js"></script> -->
<script type="text/javascript" src="./js/ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="./js/ztree/jquery.ztree.exedit-3.5.min.js"></script>
<script type="text/javascript" src="./js/ztree/jquery.ztree.exhide-3.5.js"></script>
<script type="text/javascript" src="./js/ztree/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="./js/ztree/jquery.ztree.excheck-3.5.js"></script>
</head>
<body>
	<jsp:include page="../template/head.jsp" />
	<jsp:include page="../template/leftnav.jsp" />
	<div id="page-wrapper">
		<div class="wrapper">
			<h1><a href="./jsp/illness/illness.jsp">管理员列表</a></h1><h3> <span> > </span> 管理员权限</h3>
		</div>

		<div class="i-detail i-detail-info">
			<h3>权限信息</h3>
	    	<ul id="treeDemo" class="ztree"></ul>
		</div>
		
</body>
<script type="text/javascript">
var adminCode = $.getUrlParam("adminCode");
var initData = [];
$(function (){
	$.ajax({
		url: "./adminPerssion/getAllPerssions",
		data:{},
		type: "GET",
		dataType: 'json',
		cache: false,
		success: function(ret) {
			if(ret.resultCode==0){
				zTree = $.fn.zTree.init($("#treeDemo"), setting, ret.content);
				getAdminPerssion();
			}
		}
	});
	/* getInit(); */
});

function getAdminPerssion(){
	$.ajax({
		url: "./adminPerssion/getAdminPerssion",
		data:{
			adminCode : adminCode
		},
		type: "GET",
		dataType: 'json',
		cache: false,
		success: function(ret) {
			if(ret.resultCode==0){
				var datas = [];
				var obj;
				initData = ret.content;
				for(var i=0;i<ret.content.length;i++){
					obj = {
						value : ret.content[i]
					};
					datas.push(obj);
				}
				if(datas.length>0){
					initTree(datas);
				}
			}
		}
	});
}

var zTree;

var setting = {  
        isSimpleData : true,              //数据是否采用简单 Array 格式，默认false  
        treeNodeKey : "id",               //在isSimpleData格式下，当前节点id属性  
        treeNodeParentKey : "pId",        //在isSimpleData格式下，当前节点的父节点id属性  
        showLine : true,                  //是否显示节点间的连线  
        checkable : true,//每个节点上是否显示 CheckBox
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "p", "N": "s" }
		},
		callback:{  
            onCheck:onCheck,
            onRemove : onRemove
        } 
};

var test = [{"name":"疾病管理","open":true,"id":1,"pId":null,"children":[{"id":11,"pId":1,"name":"获取疾病详情"},{"id":12,"pId":1,"name":"编辑部位"}]}];
   
function onCheck(e,treeId,treeNode){  
	var treeObj=$.fn.zTree.getZTreeObj("treeDemo");  
	var nodes=treeObj.getCheckedNodes(true);
	var currData = [];  
	for(var i=0;i<nodes.length;i++){  
		console.log(nodes[i].pId+ "----" +nodes[i].id);
		currData.push(nodes[i].id);
	}
	if(currData.length>initData.length){
		var d = getDifferent(currData,initData);
		initData = currData;
		$.ajax({
			url: "./adminPerssion/addAdminPerssion",
			data:{
				adminCode : adminCode,
				permissionid : d.join(",")
			},
			type: "GET",
			dataType: 'json',
			cache: false,
			success: function(ret) {

			}
		});
	}else if(currData.length<initData.length){
		var d = getDifferent(initData,currData);
		initData = currData;
		$.ajax({
			url: "./adminPerssion/delAdminPerssion",
			data:{
				adminCode : adminCode,
				permissionid : d.join(",")
			},
			type: "GET",
			dataType: 'json',
			cache: false,
			success: function(ret) {

			}
		});		
	}else{
		return;
	}  
}

function getDifferent(arrMax,arrMin){
	var data = [];
	for(var i=0;i<arrMax.length;i++){
		var flag = true;
		for(var j=0;j<arrMin.length;j++){
			if(arrMax[i]==arrMin[j]){
				flag = false;
			}
		}
		if(flag){
			data.push(arrMax[i]);
		}
	}
	return data;
}

function onRemove(e,treeId,treeNode){
	console.log(treeId);	
}

var result = [
	{
		value : 1
	},
	{
		value : 2
	},
	{
		value : 21
	}
];

function dealData(ret){
	var arr = ret.content;
	for(var i=0;i<arr.length;i++){
		
	}
}

function initTree(result){
    if(result.length>0){  
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");  
        var nodes=treeObj.getNodes();  
        var checkedId;  
        var nodeId;  
        for(var j=0;j<result.length;j++){  
            checkedId=result[j].value;  
            nodeId=treeObj.getNodeByParam("id", checkedId, null)  
            treeObj.checkNode(nodeId, true, true);  
            treeObj.expandNode(nodeId, true, true, true);  
            treeObj.selectNode(nodeId);  
        }  
    }  
}

/*zTree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);*/

</script>
</html>
